<?php ob_start(); 
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING']) && isset($_GET['Identifiant_direction'])) {
  $editFormAction .= "?Identifiant_direction=" . htmlentities($_GET['Identifiant_direction']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1"))
$bool=verifLibelle1('Code_direction') && verifLibelle('Nom_direction');

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")&& $bool && !existeChamp1($database_microassur, $microassur,'direction','Code_direction',@$_POST['Identifiant_direction'],@$_POST['Code_direction'])) {
  $updateSQL = sprintf("UPDATE direction SET Code_direction=%s, Nom_direction=%s WHERE Identifiant_direction=%s",
                       GetSQLValueString($_POST['Code_direction'], "text"),
                       GetSQLValueString($_POST['Nom_direction'], "text"),
                       GetSQLValueString($_POST['Identifiant_direction'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
if($Result1){
mysql_query("commit");
	//$updateGoTo = "index.php?moddirection=success";
 
 // header(sprintf("Location: %s", $updateGoTo));
      $message=colorer("La modification de la direction est prise en compte","true") ;

  }
else { mysql_query("rollback");$message=colorer("Contacter la DSI, erreur modification direction","false") ;}

  
}
elseif(isset($_POST["MM_update"])&& !$bool) 
{
if(empty($_POST['Code_direction'])) $message=colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_direction')) $message=colorer("Le code saisi comporte des caractères spéciaux interdits","false");
elseif(empty($_POST['Nom_direction'])) $message=colorer("Vous devez saisir le nom ","false");
elseif(!verifLibelle('Nom_direction')) $message=colorer("Le nom saisi comporte des caractères spéciaux interdits ","false");
else
//$message=colorer("Erreur: Au moins un champ est vide ou valeur incorrecte","false");
$message="";

}

elseif ((isset($_POST["MM_update"])) && existeChamp1($database_microassur, $microassur,'direction','Code_direction',@$_POST['Identifiant_direction'],@$_POST['Code_direction'])) $message=colorer("La direction existe déjà","false");

$Identifiant_direction_rs = "-1";
if (isset($_GET['Identifiant_direction'])) {
  $Identifiant_direction_rs = $_GET['Identifiant_direction'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs = sprintf("SELECT * FROM direction WHERE Identifiant_direction=%s", GetSQLValueString($Identifiant_direction_rs, "int"));
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>
<?php  if(isset($_GET['supp']) && $_GET['supp']=='err') $message=colorer("Contacter la DSI, erreur suppression direction","false"); ?>
<?php if(isset($_GET['suppdirection']) && $_GET['suppdirection']=='success') $message=colorer("La direction est supprimée","true"); ?>
<?php if(isset($_GET['suppdirection']) && $_GET['suppdirection']=='existregion') $message=colorer("Vous ne pouvez supprimer cette direction, au moins une région lui est rattachée","false");?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Direction";
					$nommodule2="Détail direction";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
			<div class="corps1">
				<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
							<div class="imgleft"></div>
							<div class="imgmiddle">
								<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
								<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
									<div class="formulaire">
											<table class="tabform">
												<tr>
													<td>CODE</td>
													<td><input class="resize_1" type="text" maxlength="5" name="Code_direction" value="<?php if(isset($_POST['Code_direction']))echo remind_text('Code_direction'); else echo htmlentities($row_rs['Code_direction'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
												</tr>
												<tr>
													<td>NOM</td>
													<td><input name="Nom_direction" type="text"  maxlength="30" value="<?php if(isset($_POST['Nom_direction']))echo remind_text('Nom_direction'); else echo htmlentities($row_rs['Nom_direction'], ENT_COMPAT, 'utf-8'); ?>" size="40" /></td>
												</tr>
											</table>
									</div>
							</div>
							<div class="imgright"></div>				
							<div class="cleared"></div>
				
								<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
								<div class="bt"><a <?php if(isset($row_rs['Identifiant_direction'])){ ?> href="suppdirection.php?Identifiant_direction=<?php echo $row_rs['Identifiant_direction']; ?>" <?php } else echo 'href="#"';?>><span class="btleft"></span><span class="btmiddle">Supprimer</span><span class="btright"></span></a></div>
								<div class="bt"><span class="btleft"></span><input <?php if(retourValForm('Identifiant_direction')==NULL || retourValForm('suppdirection')=='success') echo "disabled='disabled'"; ?> type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
								
							<div class="cleared"></div>
				  <input type="hidden" name="Identifiant_direction" value="<?php echo $row_rs['Identifiant_direction']; ?>" />
				  <input type="hidden" name="MM_update" value="form1" />
				</form>
			</div> 
</body>
</html>
<?php
mysql_free_result($rs);
?>
<?php ob_end_flush();?>
